home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / misc / emu / p-interp.lha / p-interp-0.4 / Memory.c < prev    next >
C/C++ Source or Header  |  2001-06-06  |  6KB  |  294 lines

  1. /*
  2.  
  3.   P-Code interpreter (to run the apple pascal system)
  4.   Copyright (C) 2000 Mario Klebsch
  5.  
  6.   This program is free software; you can redistribute it and/or modify
  7.   it under the terms of the GNU General Public License as published by
  8.   the Free Software Foundation; either version 2 of the License, or
  9.   (at your option) any later version.
  10.  
  11.   This program is distributed in the hope that it will be useful,
  12.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.   GNU General Public License for more details.
  15.  
  16.   You should have received a copy of the GNU General Public License
  17.   along with this program; if not, write to the Free Software
  18.   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.   $Log: Memory.c,v $
  21.   Revision 1.5  2001/06/06 23:26:44  mario
  22.   Amiga-Patch von "Stefan A. Haubenthal" <polluks@web.de>
  23.  
  24.   Revision 1.4  2001/05/26 15:13:29  mario
  25.   Diverse kleine Fehler behoben, fehlende #includes, Labels ohne Statement
  26.   dahinter, ...
  27.  
  28.   Revision 1.3  2001/05/21 20:47:06  mario
  29.   Die einzelnen Bits der Speicherflags können jetzt einzeln abbestellt
  30.   werden.
  31.  
  32.   Revision 1.2  2001/05/20 13:12:02  mario
  33.   CVS-Idents und Logs eingefügt
  34.  
  35.  
  36. */
  37.  
  38. #ident "$Id: Memory.c,v 1.5 2001/06/06 23:26:44 mario Exp $";
  39.  
  40. #include <stdio.h>
  41. #include <ctype.h>
  42. #include <string.h>
  43.  
  44. #include "psystem.h"
  45. #include "pcode.h"
  46. #include "Memory.h"
  47.  
  48. #ifdef XXX
  49. #define MEM_VALID    1
  50. #define MEM_RD_ONLY    2
  51. #define MEM_WARN    4
  52. #endif
  53.  
  54. int SwapBytes=0;
  55.  
  56. typedef struct Memory_St
  57. {
  58. #ifdef WORD_MEMORY
  59.   word        Value;
  60. #else
  61.   byte        Value;
  62. #endif
  63.   byte        Flags;
  64. } Memory_t;
  65.  
  66. #ifdef __SASC
  67. __far
  68. #endif
  69. Memory_t    Mem[0x10000];
  70.  
  71. #ifdef WORD_MEMORY
  72. word MemRd(word Addr)
  73. {
  74.   PointerCheck(Addr);
  75. #ifdef MEM_VALID
  76.   if (!(Mem[Addr].Flags&MEM_VALID))
  77.     warning("MemRdByte: Reading uninitialized Memory 0x%04x",Addr);
  78. #endif
  79. #ifdef MEM_WARN
  80.   if (Mem[Addr].Flags&MEM_WARN)
  81.     warning("MemRdByte: Access to 0x%04x", Addr);
  82. #endif
  83.   return(Mem[Addr].Value);
  84. }
  85.  
  86. void MemWr(word Addr, word Value)
  87. {
  88.   PointerCheck(Addr);
  89. #ifdef MEM_RD_ONLY
  90.   if (Mem[Addr].Flags&MEM_RD_ONLY)
  91.     {
  92.       warning("MemWrByte: 0x%04x is read only", Addr);
  93.       XeqError(XBADMEM);
  94.     }
  95. #endif
  96. #ifdef MEM_WARN
  97.   if (Mem[Addr].Flags&MEM_WARN)
  98.     warning("MemWrByte: Access to 0x%04x", Addr);
  99. #endif
  100.   Mem[Addr].Value=Value;
  101. #ifdef MEM_VALID
  102.   Mem[Addr].Flags|=MEM_VALID;
  103. #endif
  104. }
  105.  
  106. byte MemRdByte(word Addr, Integer Offset)
  107. {
  108.   PointerCheck(Addr);
  109.   Addr   += (Offset&~1)/2;
  110.   Offset &= 1;
  111.   if (Offset)
  112.     return(MemRd(Addr)>>8);
  113.   else
  114.     return(MemRd(Addr)&0xff);
  115. }
  116.  
  117. void MemWrByte(word Addr, Integer Offset, byte Value)
  118. {
  119.   word    w;
  120.   PointerCheck(Addr);
  121.   Addr   += (Offset&~1)/2;
  122.   Offset &= 1;
  123.   w=MemRd(Addr);
  124.   if (Offset)
  125.     w=(w&0x00ff)|(Value<<8);
  126.   else
  127.     w=(w&0xff00)|(Value&0xff);
  128.   MemWr(Addr, w);
  129. }
  130.  
  131. #else
  132.  
  133. byte MemRdByte(word Addr, Integer Offset)
  134. {
  135.   PointerCheck(Addr);
  136.   Addr += Offset;
  137. #ifdef MEM_VALID
  138.   if (!(Mem[Addr].Flags&MEM_VALID))
  139.     warning("MemRdByte: Reading uninitialized Memory 0x%04x",Addr);
  140. #endif
  141. #ifdef MEM_WARN
  142.   if (Mem[Addr].Flags&MEM_WARN)
  143.     warning("MemRdByte: Access to 0x%04x", Addr);
  144. #endif
  145.   return(Mem[Addr].Value);
  146. }
  147.  
  148. void MemWrByte(word Addr, Integer Offset, byte Value)
  149. {
  150.   PointerCheck(Addr);
  151.   Addr += Offset;
  152. #ifdef MEM_RD_ONLY
  153.   if (Mem[Addr].Flags&MEM_RD_ONLY)
  154.     {
  155.       warning("MemWrByte: 0x%04x is read only", Addr);
  156.       XeqError(XBADMEM);
  157.     }
  158. #endif
  159. #ifdef MEM_WARN
  160.   if (Mem[Addr].Flags&MEM_WARN)
  161.     warning("MemWrByte: Access to 0x%04x", Addr);
  162. #endif
  163.   Mem[Addr].Value=Value;
  164. #ifdef MEM_VALID
  165.   Mem[Addr].Flags|=MEM_VALID;
  166. #endif
  167. }
  168.  
  169. word MemRd(word Addr)
  170. {
  171.   PointerCheck(Addr);
  172.   return ( MemRdByte(Addr, 0+SwapBytes) +
  173.        (MemRdByte(Addr, 1-SwapBytes)<<8) );    
  174. }
  175.  
  176. void MemWr(word Addr, word Value)
  177. {
  178.   PointerCheck(Addr);
  179.   MemWrByte(Addr, 0+SwapBytes, Value & 0xff);
  180.   MemWrByte(Addr, 1+SwapBytes, Value >> 8);
  181. }
  182. #endif
  183.  
  184. void MemReadOnly(word from, word to, int RO)
  185. {
  186. #ifdef MEM_RD_ONLY
  187.   while (from<=to)
  188.     {
  189.       if (RO)
  190.     Mem[from].Flags|=MEM_RD_ONLY;
  191.       else
  192.     Mem[from].Flags&=~MEM_RD_ONLY;
  193.       from++;
  194.     }
  195. #endif
  196. }
  197.  
  198. void MemDump(FILE *f, word Start, word End)
  199. {
  200.   int    w;
  201.   int    i;
  202.   int    Count=0;
  203. #ifdef WORD_MEMORY
  204.   word    Value;
  205.   char    b[6];
  206.   byte    ch1;
  207. #else
  208.   byte    Value;
  209.   char    b[4];
  210. #endif
  211.   byte    ch;
  212.   char    Buffer[80];
  213.   char    OldBuffer[80];
  214.   OldBuffer[0]='\0';
  215.   for (w=Start; w<=End;)
  216.     {
  217. #ifdef WORD_MEMORY
  218.       w=w&0xfff8;
  219. #else
  220.       w=w&0xfff0;
  221. #endif
  222.       sprintf(Buffer,"%04x: %48s %16s\n",w,"","");
  223. #ifdef WORD_MEMORY
  224.       for (i=0;i< 8;i++)
  225. #else
  226.       for (i=0;i<16;i++)
  227. #endif
  228.     {
  229. #ifdef MEM_VALID
  230.       if (Mem[w].Flags&MEM_VALID)
  231.         {
  232. #endif /* MEM_VALID */
  233.           Value=Mem[w].Value;
  234. #ifdef WORD_MEMORY
  235.           sprintf(b,"%04x",Value);
  236. #else
  237.           sprintf(b,"%02x",Value);
  238. #endif
  239.           ch=Value&0xff;
  240.           if (!isprint(ch))
  241.         ch='.';
  242. #ifdef WORD_MEMORY
  243.           ch1=Value>>8;
  244.           if (!isprint(ch1))
  245.         ch1='.';
  246. #endif
  247. #ifdef MEM_VALID
  248.         }
  249.       else
  250.         {
  251. #ifdef WORD_MEMORY
  252.           strcpy(b,"....");
  253.           ch1=' ';
  254. #else
  255.           strcpy(b,"..");
  256. #endif
  257.           ch=' ';
  258.         }
  259. #endif /* MEM_VALID */
  260. #ifdef WORD_MEMORY
  261.       Buffer[6+5*i   ]=b[0];
  262.       Buffer[6+5*i+1 ]=b[1];
  263.       Buffer[6+5*i+2 ]=b[2];
  264.       Buffer[6+5*i+3 ]=b[3];
  265.       Buffer[6+5*8+2*i  ]=ch;
  266.       Buffer[6+5*8+2*i+1]=ch1;
  267. #else
  268.       Buffer[6+3*i   ]=b[0];
  269.       Buffer[6+3*i+1 ]=b[1];
  270.       Buffer[6+3*16+i]=ch;
  271. #endif
  272.       w++;
  273.     }
  274.       if (strcmp(Buffer+4, OldBuffer+4)!=0)
  275.     {
  276.       if (Count>1)
  277.         fprintf(f, ".... %d line%s omitted\n",Count-1, Count==2?"":"s");
  278.       if (Count>0)
  279.         fputs(OldBuffer, f);
  280.       Count=0;
  281.       fputs(Buffer, f);
  282.     }
  283.       else
  284.     Count++;
  285.       strcpy(OldBuffer,Buffer);
  286.     }
  287. }
  288.  
  289. void MemInit(void)
  290. {
  291.   memset(Mem, 0, sizeof(Mem));
  292. }
  293.  
  294.